package com.library.workbench.web.controller.user;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.DateTimeUtils;
import com.library.commons.utils.UUIDUtils;
import com.library.settings.pojo.User;
import com.library.settings.service.UserService;
import com.library.workbench.pojo.Book;
import com.library.workbench.pojo.Borrow;
import com.library.workbench.service.BookService;
import com.library.workbench.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    @RequestMapping("/workbench/user/borrow/toBorrowInfo.do")
    public String toBorrowInfo(){
        return "workbench/user/borrow/borrow";
    }

    @RequestMapping("/workbench/user/borrow/queryExistedBookByActNameAuthor.do")
    @ResponseBody
    public Object queryExistedBookByActNameAuthor(String bookAccount,String bookName,String author,Integer pageNo,Integer pageSize){
        // 封装对象
        Map<String,Object> map=new HashMap<>();
        map.put("bookName",bookName);
        map.put("author",author);
        map.put("bookAccount",bookAccount);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        // 调用service层方法
        List<Book> books = borrowService.queryExistBookForConditionForPage(map);
        int totalRows = borrowService.queryExistBookByConditionForRows(map);
        // 封装参数
        map.put("books",books);
        map.put("totalRows",totalRows);
        return map;
    }

    @RequestMapping("/workbench/user/borrow/queryBookInformationByBookId.do")
    @ResponseBody
    public Object queryBookInformationByBookId(String bookId){
        // 调用service层方法查询书籍信息
        Book book = bookService.queryBookByBookId(bookId);
        // 生成响应信息
        return book;
    }

    @RequestMapping("/workbench/user/borrow/insertBorrow.do")
    @ResponseBody
    public Object insertBorrow(String bookId, String endDate, HttpSession session){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        // 封装参数
        Borrow borrow=new Borrow();
        borrow.setBorrowId(UUIDUtils.getUUID());
        borrow.setBookId(bookId);
        borrow.setBorrowDate(DateTimeUtils.formatDate(new Date()));
        borrow.setEndTime(endDate);
        borrow.setUserId(user.getUserId());
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法
            int res = borrowService.saveBorrow(borrow);
            Book book = bookService.queryBookByBookId(bookId);
            book.setExist("0");
            res+=bookService.updateBookByBookId(book);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }
}
